import Cookies from 'js-cookie'
// cookie中操作token

const TokenKey = 'admin' // cookie中存储token的字段名
const TimeKey = 'admin-token-time' // token失效
/**
 * cookie中获取token
 * @returns
 */
export function getToken() {
  return Cookies.get(TokenKey)
}

export function getUser() {
  if (!localStorage.getItem('device-data-user')) return {}
  else return JSON.parse(localStorage.getItem('device-data-user'))
}

export function setUser(userInfo) {
  const user = JSON.stringify(userInfo)
  localStorage.setItem('device-data-user', user)
}

export function removeUser() {
  localStorage.removeItem('device-data-user')
}

/**
 * 存储token到cookie中
 * @param {*} token token
 * @returns
 */
export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

/**
 * cookie中删除token
 * @returns
 */
export function removeToken() {
  return Cookies.remove(TokenKey)
}

/**
 * 设置token初始时间
 * @param {*} time 时间戳
 */
export function setTime(time) {
  Cookies.set(TimeKey, time)
}

/**
 * 获取token初始时间
 * @returns
 */
export function getTime() {
  return Cookies.get(TimeKey)
}
